<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片转3D - 真实感数字人</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #87CEEB 0%, #98FB98 50%, #F0E68C 100%);
            overflow: hidden;
        }

        #container {
            position: relative;
            width: 100vw;
            height: 100vh;
        }

        #canvas-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        #ui-panel {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(255, 255, 255, 0.95);
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
            min-width: 400px;
        }

        .input-group {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
        }

        #textInput {
            flex: 1;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 25px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }

        #textInput:focus {
            border-color: #FF69B4;
        }

        #speakBtn {
            padding: 12px 25px;
            background: linear-gradient(45deg, #FF69B4, #FF1493);
            color: white;
            border: none;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
        }

        #speakBtn:hover {
            transform: translateY(-2px);
        }

        #speakBtn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .status {
            text-align: center;
            color: #666;
            font-size: 14px;
            margin-top: 10px;
        }

        .loading {
            display: none;
            text-align: center;
            color: #FF69B4;
        }

        .loading.show {
            display: block;
        }

        #controls {
            position: absolute;
            top: 20px;
            right: 20px;
            background: rgba(255, 255, 255, 0.9);
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .control-group {
            margin-bottom: 10px;
        }

        .control-group label {
            display: block;
            margin-bottom: 5px;
            font-size: 12px;
            color: #666;
        }

        .control-group input[type="range"] {
            width: 100%;
        }

        #title {
            position: absolute;
            top: 20px;
            left: 20px;
            color: white;
            font-size: 24px;
            font-weight: bold;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }

        .feature-info {
            position: absolute;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(255, 255, 255, 0.9);
            padding: 10px 20px;
            border-radius: 20px;
            color: #333;
            font-size: 14px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .image-upload {
            position: absolute;
            top: 80px;
            left: 20px;
            background: rgba(255, 255, 255, 0.9);
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .image-upload input[type="file"] {
            margin-bottom: 10px;
        }

        .image-upload button {
            padding: 8px 15px;
            background: linear-gradient(45deg, #4CAF50, #45a049);
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 12px;
        }

        .image-upload button:hover {
            background: linear-gradient(45deg, #45a049, #4CAF50);
        }
    </style>
</head>
<body>
    <div id="container">
        <h1 id="title">图片转3D数字人</h1>
        
        <div class="feature-info">
            🖼️ 基于真实图片 🎭 3D重建技术 ✨ 真实感材质 🌸 自然动画
        </div>
        
        <div class="image-upload">
            <h4>上传图片</h4>
            <input type="file" id="imageUpload" accept="image/*">
            <button onclick="loadImage()">加载图片</button>
            <div id="imageStatus" style="font-size: 12px; color: #666; margin-top: 5px;"></div>
        </div>
        
        <div id="canvas-container"></div>
        
        <div id="controls">
            <div class="control-group">
                <label>旋转速度</label>
                <input type="range" id="rotationSpeed" min="0" max="2" step="0.1" value="1">
            </div>
            <div class="control-group">
                <label>动画强度</label>
                <input type="range" id="animationIntensity" min="0" max="2" step="0.1" value="1">
            </div>
            <div class="control-group">
                <label>表情强度</label>
                <input type="range" id="expressionIntensity" min="0" max="2" step="0.1" value="1">
            </div>
        </div>
        
        <div id="ui-panel">
            <div class="input-group">
                <input type="text" id="textInput" placeholder="输入要说的内容..." maxlength="200">
                <button id="speakBtn">说话</button>
            </div>
            <div class="loading" id="loading">
                <div>正在生成语音...</div>
            </div>
            <div class="status" id="status">准备就绪 - 请上传图片开始</div>
        </div>
    </div>

    <script src="/socket.io/socket.io.js"></script>
    <script src="js/three.min.js"></script>
    <script src="js/gsap.min.js"></script>
    <script src="js/image-to-3d.js"></script>
    <script>
        let digitalHuman = null;
        let currentImageUrl = 'image.png'; // 默认使用项目中的图片

        // 检查依赖是否加载
        window.addEventListener('load', function() {
            console.log('页面加载完成');
            
            if (typeof THREE === 'undefined') {
                console.error('Three.js未加载');
                document.getElementById('status').textContent = '错误：Three.js未加载';
                return;
            }
            
            if (typeof gsap === 'undefined') {
                console.error('GSAP未加载');
                document.getElementById('status').textContent = '错误：GSAP未加载';
                return;
            }
            
            console.log('所有依赖已加载');
            
            // 初始化3D模型
            init3DModel();
        });

        function init3DModel() {
            const container = document.getElementById('canvas-container');
            if (!container) {
                console.error('找不到canvas-container元素');
                return;
            }
            
            try {
                digitalHuman = new ImageTo3D(container, currentImageUrl);
                console.log('图片转3D模型已创建');
                document.getElementById('status').textContent = '3D模型已加载 - 基于图片重建';
            } catch (error) {
                console.error('创建3D模型失败:', error);
                document.getElementById('status').textContent = '3D模型创建失败';
            }
        }

        function loadImage() {
            const fileInput = document.getElementById('imageUpload');
            const file = fileInput.files[0];
            
            if (!file) {
                document.getElementById('imageStatus').textContent = '请选择图片文件';
                return;
            }
            
            if (!file.type.startsWith('image/')) {
                document.getElementById('imageStatus').textContent = '请选择有效的图片文件';
                return;
            }
            
            const reader = new FileReader();
            reader.onload = function(e) {
                currentImageUrl = e.target.result;
                document.getElementById('imageStatus').textContent = '图片已加载，正在重建3D模型...';
                
                // 重新创建3D模型
                setTimeout(() => {
                    if (digitalHuman) {
                        digitalHuman.dispose();
                    }
                    init3DModel();
                    document.getElementById('imageStatus').textContent = '3D模型重建完成！';
                }, 1000);
            };
            
            reader.readAsDataURL(file);
        }

        // 绑定控制事件
        document.addEventListener('DOMContentLoaded', function() {
            const rotationSpeed = document.getElementById('rotationSpeed');
            const animationIntensity = document.getElementById('animationIntensity');
            const expressionIntensity = document.getElementById('expressionIntensity');
            
            function updateControls() {
                if (digitalHuman) {
                    digitalHuman.updateControls(
                        parseFloat(rotationSpeed.value),
                        parseFloat(animationIntensity.value),
                        parseFloat(expressionIntensity.value)
                    );
                }
            }
            
            rotationSpeed.addEventListener('input', updateControls);
            animationIntensity.addEventListener('input', updateControls);
            expressionIntensity.addEventListener('input', updateControls);
            
            // 说话功能
            const speakBtn = document.getElementById('speakBtn');
            const textInput = document.getElementById('textInput');
            
            speakBtn.addEventListener('click', handleSpeak);
            textInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    handleSpeak();
                }
            });
        });

        function handleSpeak() {
            const textInput = document.getElementById('textInput');
            const text = textInput.value.trim();
            
            if (!text) {
                document.getElementById('status').textContent = '请输入要说的内容';
                return;
            }
            
            if (!digitalHuman) {
                document.getElementById('status').textContent = '3D模型未加载';
                return;
            }
            
            // 显示加载状态
            document.getElementById('loading').classList.add('show');
            document.getElementById('status').textContent = '正在生成语音...';
            
            // 禁用按钮
            const speakBtn = document.getElementById('speakBtn');
            speakBtn.disabled = true;
            
            // 模拟TTS处理
            setTimeout(() => {
                // 开始说话动画
                digitalHuman.startSpeaking(3);
                
                // 隐藏加载状态
                document.getElementById('loading').classList.remove('show');
                document.getElementById('status').textContent = `正在说话: "${text}"`;
                
                // 启用按钮
                speakBtn.disabled = false;
                
                // 3秒后重置状态
                setTimeout(() => {
                    document.getElementById('status').textContent = '准备就绪';
                }, 3000);
            }, 1000);
        }
    </script>
</body>
</html>
